package org.cnc.explain.lifecycle;

import org.cnc.explain.lifecycle.beanparse_2.ParsedAnnotationBean;
import org.cnc.explain.lifecycle.beanparse_2.ParsedAnnotationDependenceBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.core.io.ClassPathResource;

/**
 * <p>
 * #TODO: desc
 * </p>
 *
 * @author zhiyong.tan
 * @since 2022-08-25
 */
public class Step2BeanDefinitionParse {
	@Test
	public void testParseFromXmlFile() {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		int count = reader.loadBeanDefinitions(new ClassPathResource("step2.xml"));
		System.out.println("total load " + count + " bean definitions");

		for (String definitionName : factory.getBeanDefinitionNames()) {
			BeanDefinition definition = factory.getBeanDefinition(definitionName);
			System.out.println(definition);
		}
	}

	@Test
	public void testParseFromAnnotationConfiguration() {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(factory);
		// 注册到 bean-factory
		reader.register(ParsedAnnotationBean.class); // 依赖bean
		reader.register(ParsedAnnotationDependenceBean.class);			 //  被注入bean

		//resource
		factory.addBeanPostProcessor(factory.getBean(CommonAnnotationBeanPostProcessor.class));
		//autowired
		factory.addBeanPostProcessor(factory.getBean(AutowiredAnnotationBeanPostProcessor.class));

		ParsedAnnotationBean bean = factory.getBean(ParsedAnnotationBean.class);
		System.out.println(bean);
	}

}
